package com.view.jurisdictionmanage;

import com.bean.Role;
import com.bean.User;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoTable;
import com.service.RoleService;
import com.service.UserService;
import com.service.impl.RoleServiceImpl;
import com.service.impl.UserServiceImpl;
import com.util.PageUtilSuper;
import com.view.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class RoleJurManagePanel extends JPanel {
    private MainFrame frame;

    private LovoTable roleTable;

    private LovoButton addRoleBtn;
    private LovoButton delRoleBtn;
    private LovoButton showRoleBtn;
    private LovoButton roleQueryBtn;


    private LovoButton prev,next;

    private JLabel txtPage;
    private PageUtilSuper<Role> pageUtilSuper;
    private int pgNumber = 1;
    private int pageSize = 3;

    private RoleService roleService = new RoleServiceImpl();

    public RoleJurManagePanel(MainFrame frame) {
        this.frame = frame;
        this.setBackground(Color.WHITE);
        setPageUtilSuper();
        this.setLayout(null);
        this.initTable();
        this.initBtn();

        txtPage = new JLabel();
        this.txtPage.setBounds(170, 305, 100, 50);
        this.add(this.txtPage);
        labelInit();
    }

    private void initTable() {
        // TODO Auto-generated method stub
        this.roleTable = new LovoTable(this, new String[]{"角色名称"}, new String[]{"roleName"},// 数组中应该是对应属性的属性名
                "id");// 填入唯一标示属性
        List<Role> roleList = new ArrayList<>();
        roleList = pageUtilSuper.getPgNumberThing(1);
        roleTable.updateLovoTable(roleList);
        roleTable.setSizeAndLocation(0, 0, 800, 300);
    }

    private void initBtn() {
        // TODO Auto-generated method stub
        this.addRoleBtn = new LovoButton("添加新角色", 135, 400, this);
        this.delRoleBtn = new LovoButton("删除角色", 235, 400, this);
        this.showRoleBtn = new LovoButton("查看角色权限", 335, 400, this);
        this.roleQueryBtn = new LovoButton("修改角色权限", 435, 400, this);
        this.prev = new LovoButton("上一页",30,320,this);
        this.next = new LovoButton("下一页",250,320,this);

        next.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pageUtilSuper.haveNext(pgNumber)) {
                    pgNumber ++;
                    flush();
                } else {
                    JOptionPane.showMessageDialog(null, "当前是最后一页");
                }
            }
        });

        prev.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pageUtilSuper.havePrev(pgNumber)) {
                    pgNumber--;
                    flush();
                } else {
                    JOptionPane.showMessageDialog(null, "当前是第一页");
                }
            }
        });

        showRoleBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = roleTable.getKey();
                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选择一行");
                }else{
                    RoleShowDialog roleShowDialog = new RoleShowDialog(frame,key);
                }
            }
        });

        addRoleBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RoleAddDialog roleAddDialog = new RoleAddDialog(frame);
            }
        });

        delRoleBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = roleTable.getKey();
                if(key == -1 ){
                    JOptionPane.showMessageDialog(null,"请先选择一行");
                }else {
                    int m = JOptionPane.showConfirmDialog(null,"你确定要删除吗？");
                    if(m == 0 ){
                        if(roleService.deleteRole(key)){
                            JOptionPane.showMessageDialog(null,"删除成功");
                            frame.flush();
                        }else {
                            JOptionPane.showMessageDialog(null,"还有用户使用该角色,删除失败");
                        }
                    }
                }
            }
        });

        roleQueryBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = roleTable.getKey();
                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选择一行");
                }else{
                    RoleUpdateDialog roleUpdateDialog = new RoleUpdateDialog(frame,key);
                }
            }
        });

    }

    private void labelInit() {
        int maxPage = pageUtilSuper.getMaxNumber();
        this.txtPage.setText(pgNumber+" / "+maxPage);
    }

    public void setPageUtilSuper() {
        List<Role> roleList = new ArrayList<>();
        roleList = roleService.getAllRole();
        pageUtilSuper = new PageUtilSuper(pageSize,roleList);
    }

    public void flush(){
        tableFlush();
        labelInit();
    }

    public void tableFlush(){
        setPageUtilSuper();
        List<Role> roleList = new ArrayList<>();
        roleList = pageUtilSuper.getPgNumberThing(pgNumber);
        roleTable.updateLovoTable(roleList);
    }

}
